package common;

import android.annotation.TargetApi;
import android.content.Context;
import android.os.Build;
import android.os.Bundle;
import android.support.v4.app.FragmentActivity;
import android.view.KeyEvent;
import android.view.View;
import android.view.ViewGroup;
import android.view.Window;
import android.view.WindowManager;
import android.view.inputmethod.InputMethodManager;
import android.widget.Button;
import android.widget.RelativeLayout;
import android.widget.TextView;
import android.widget.Toast;

import com.example.mylibrary.R;

import ui.LoadView;
import utils.SystemBarTintManager;

/**
 * Created by YL on 2016/9/29.
 */

public abstract class BaseActivity extends FragmentActivity {
    BaseActivity mBaseActivity;
    private Button back_bt;
    private TextView title_tv;
    private RelativeLayout content;
    private TextView right;
    private RelativeLayout header_lt;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
//        requestWindowFeature(Window.FEATURE_NO_TITLE);
        super.onCreate(savedInstanceState);
//        SoftInputUtil.hideSoftInputMode(this);
        mBaseActivity = this;
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) { //系统版本大于19
            setTranslucentStatus(true);
        }
        SystemBarTintManager tintManager = new SystemBarTintManager(this);
        tintManager.setStatusBarTintEnabled(true);
        tintManager.setStatusBarTintResource(R.color.blue);//设置标题栏颜色，此颜色在color中声明
    }

    @TargetApi(19)
    private void setTranslucentStatus(boolean on) {
        Window win = getWindow();
        WindowManager.LayoutParams winParams = win.getAttributes();
        final int bits = WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS;
        if (on) {
            winParams.flags |= bits;        // a|=b的意思就是把a和b按位或然后赋值给a   按位或的意思就是先把a和b都换成2进制，然后用或操作，相当于a=a|b
        } else {
            winParams.flags &= ~bits;        //&是位运算里面，与运算  a&=b相当于 a = a&b  ~非运算符
        }
        win.setAttributes(winParams);
    }

    @Override
    public void setContentView(int layoutResID) {
        setContentView(mBaseActivity.getLayoutInflater().inflate(layoutResID, null));
    }

    @Override
    public void setContentView(View view) {
        if (view == null) {
            try {
                throw new NullPointerException("view is null exception");
            } catch (Exception e) {
                return;
            }
        }
        setContentView(view, new ViewGroup.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.MATCH_PARENT));
    }

    private ViewGroup rootView = null;

    @Override
    public void setContentView(View view, ViewGroup.LayoutParams params) {
        if (view == null) {
            try {
                throw new NullPointerException("view is null exception");
            } catch (Exception e) {
                return;
            }
        }
        rootView = (ViewGroup) mBaseActivity.getLayoutInflater().inflate(R.layout.hader_xml, null);
        header_lt = (RelativeLayout) rootView.findViewById(R.id.header_lt);
        title_tv = (TextView) rootView.findViewById(R.id.title_tv);
        right = (TextView) rootView.findViewById(R.id.right);
        back_bt = (Button) rootView.findViewById(R.id.back);
        mLoadView = (LoadView) rootView.findViewById(R.id.downLoadStatusBox);
        content = (RelativeLayout) rootView.findViewById(R.id.public_content_layout);
        RelativeLayout.LayoutParams mLayoutParams = new RelativeLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.MATCH_PARENT);
        content.addView(view, mLayoutParams);
        super.setContentView(rootView);
        initTitlle();
    }

    /**
     * 隐藏标题栏
     */
    public void gone(int i) {
        header_lt.setVisibility(i);
    }

    /**
     * @param color 设置背景颜色
     */
    public void setBack_groud_text(int color) {
        if (content != null)
            content.setBackgroundResource(color);
    }

    View.OnClickListener myOnClickListener_zuo = null;
    View.OnClickListener myOnClickListener_you = null;

    /**
     * @param onclick_zuo 设置返回按钮的事件
     */
    public void setOnclick_zuo(View.OnClickListener onclick_zuo) {
        this.myOnClickListener_zuo = onclick_zuo;
    }

    public void setProgressTxt(String txt){
        mLoadView.setTextMessage(txt);
    }
    public void setOnclick_you(View.OnClickListener onclick_you) {
        this.myOnClickListener_you = onclick_you;
    }

    /**
     * 设置返回按钮的事件 和 默认事件
     */
    public void initTitlle() {
        if (myOnClickListener_zuo != null) {
            back_bt.setOnClickListener(myOnClickListener_zuo);
        } else {
            back_bt.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View view) {
//                    ActivityManagerUtils.getInstance().finishActivity();
                    BaseActivity.this.finish();
                }
            });
        }
        if (myOnClickListener_you != null) {
            right.setOnClickListener(myOnClickListener_you);
        }
    }

    public void setTitle_text(String string) {
        title_tv.setText(string);
    }

    LoadView mLoadView = null;

    /**
     * 显示进度条
     */
    public void showProgressDialog() {
        if (!mLoadView.isLoading()) {
            mLoadView.startLoad();
        }
    }

    public void showProgressDialog(String msg) {
        if (!mLoadView.isLoading()) {
            mLoadView.startLoad();
            mLoadView.setTextMessage(msg);
        }
    }

    /**
     * 隐藏进度条
     */
    public void cancelProgressDialog() {
        if (mLoadView.isLoading()) {
            mLoadView.stopLoad();
        }
    }


    @Override
    protected void onPause() {
        super.onPause();
        try {
            InputMethodManager imm = (InputMethodManager) this.getSystemService(Context.INPUT_METHOD_SERVICE);
            if (imm != null) {
                imm.hideSoftInputFromWindow(this.getWindow().getCurrentFocus().getWindowToken(), 0);
            }
        } catch (Exception ex) {

        }
    }

    /**
     * 是否隐藏左边按钮
     */
    public void visibility_zuo(int i) {
        back_bt.setVisibility(i);
    }

    /**
     * 是否隐藏右边按钮
     */
    public void visibility_yuo(int i) {
        right.setVisibility(i);
    }

    /**
     * 设置右边按钮文字
     */
    public void settext_yuo(String i) {
        right.setText(i);
    }

    /**
     * 设置右边按钮图片
     */
    public void setImage_yuo(int i) {
        right.setBackgroundResource(i);
    }

    /**
     * 设置右边按钮文字颜色
     */
    public void settext_color_yuo(int i) {
        right.setTextColor(i);
    }

    /**
     * 打印toast
     */
    public void showToast(String str) {
        Toast.makeText(this, str, Toast.LENGTH_SHORT).show();
    }

    @Override
    public boolean onKeyDown(int keyCode, KeyEvent event) {
        if (keyCode == KeyEvent.KEYCODE_BACK) {
            finish();
        }
        return true;
    }
}
